﻿using DeviceMonitor.Utils;
using DeviceMonitor.ViewModel;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DeviceMonitor.Windows
{
    /// <summary>
    /// BillingEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class BillingEditWindow : Window
    {
        BillingEditViewModel viewModel;
        public BillingEditWindow()
        {
            InitializeComponent();
            viewModel = new BillingEditViewModel();
            viewModel.record = new Model.Billing();
            viewModel.record.status = "0";

            this.DataContext = viewModel;
        }

        private void Button_Click_Save(object sender, RoutedEventArgs e)
        {
            JObject paramJSON = JObject.FromObject(viewModel.record);
            //发送http请求
            string response = HttpPostHelper.doHttpPost("http://" + App.GetConfig().HttpServerIp + ":" + App.GetConfig().HttpServerPort + "/billing/add", paramJSON.ToString());
            JObject respJson = JObject.Parse(response);
            string status = respJson["success"].ToString();
            if (status.ToLower() != "true")
            {
                MessageBox.Show(respJson["message"].ToString());
                return;
            }
            this.DialogResult = true;
            MessageBox.Show("保存成功");
            this.Close();
        }
    }
}
